package cn.com.dycc.sys_manage.deptsysmanagement.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dycc.common.controller.BaseController;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.Dept;
import cn.com.dycc.sys_manage.deptsysmanagement.entity.DeptResult;
import cn.com.dycc.sys_manage.deptsysmanagement.service.DeptService;
import cn.com.dycc.util.JsonObjectResult;
/**
 * 单位/部门管理控制层
 * Description: 
 * Author: linfangan
 * Version: 1.0
 * Create Date Time: 2017年8月15日 下午3:45:49.
 * Update Date Time: 
 * @see
 */
@Controller
@RequestMapping("/dept")
public class DeptController extends BaseController{
	
	@Resource(name="deptService")
	private DeptService deptService;
	
//	@SuppressWarnings("unchecked")
//	@ResponseBody
//	@RequestMapping("/adddept.do")
//	public JsonResult addDept(Dept dept,HttpServletRequest request){
//		List<Integer> bindRoleNo =(List<Integer>)request.getSession().getAttribute("role_no");
//		Dept addDept = deptService.addDept(dept,bindRoleNo);
//		return new JsonResult(addDept);
//	}
	
	//用于单位的树状图显示,全查,包含不启用的状态
	//管理员显示  20180503管理员显示部门更改
	@RequestMapping("/findalldept.do")
	@ResponseBody
	public JsonObjectResult findAllDept(HttpServletRequest request,String send_dept_id){
//		String org_id = request.getSession().getAttribute("org_id").toString();
		List<Object> list = deptService.findAllDeptNew(request,send_dept_id);		
		return new JsonObjectResult(list);
	}
	
	//更新/增加部门单位 
	//20180504部门的增加和更新进行更改
	@RequestMapping("/updept.do")
	@ResponseBody
	public JsonObjectResult updateDept(HttpServletRequest request,Dept dept){
//		String org_id = request.getSession().getAttribute("org_id").toString();
//		String update_user_id = request.getSession().getAttribute("id").toString();
//		dept.setOrg_id(org_id);
//		dept.setUpdate_user_id(update_user_id);
//		System.out.println("dept2="+dept);
		String up = deptService.addAndUpdate(request, dept);
		return new JsonObjectResult(up);
	}
	
	//通过单位/部门Id查找信息
	@RequestMapping("/seldept.do")
	@ResponseBody
	public JsonObjectResult findDeptByDeptNo(HttpServletRequest request,String dept_no){
		DeptResult dept = deptService.findAllInfoByDeptNo(request,dept_no);
		return new JsonObjectResult(dept);
	}
	
//	//根据数组id删除部门单位
//	@RequestMapping("/deldept.do")
//	@ResponseBody
//	public JsonResult deleteDeptsByIds(String...ids){
//		String message = deptService.deleteDeptsByIds(ids);
//		return new JsonResult(message);
//	}
	
	//根据id删除部门单位
	@RequestMapping("/cleardept.do")
	@ResponseBody
	public JsonObjectResult clearDeptBydeptNo(HttpServletRequest request,String dept_no){
		String message = deptService.deleteDeptByDeptNo(request,dept_no);
		return new JsonObjectResult(message);
	}
	
	
//	//批量插入单位/部门
//	@SuppressWarnings("unchecked")
//	@RequestMapping("/addalldept.do")
//	@ResponseBody
//	public JsonResult insertBatchDept(List<Dept> listDept,HttpServletRequest request){
//		List<Integer> bindRoleNo =(List<Integer>)request.getSession().getAttribute("role_no");
//		List<Dept> allDept = deptService.insertBatchDept(listDept, bindRoleNo);
//		return new JsonResult(allDept);
//	}
	
//	//查找全部的单位/部门信息/用于下拉框单位/部门显示/用户管理员需要有该权限
//	@SuppressWarnings("unchecked")
//	@RequestMapping("/findall.do")
//	@ResponseBody
//	public JsonResult findAll(HttpServletRequest request){
//		List<Integer> bindRoleNo =(List<Integer>)request.getSession().getAttribute("role_no");
//		Integer bindTop_dept_no = (Integer)request.getSession().getAttribute("top_dept_no");
//		List<Dept> allDept = deptService.findAll(bindRoleNo,bindTop_dept_no);
//		return new JsonResult(allDept);
//	}
	
//	//查询id最大值
//	@ResponseBody
//	@RequestMapping("/findMaxId.do")
//	public JsonResult findMaxId(){
//		int maxId = deptService.findMaxId();
//		return new JsonResult(maxId);
//	}
	
//	//获取id
//	@ResponseBody
//	@RequestMapping("/getdeptno.do")
//	public JsonResult getDeptNo(){
//		int deptNo = deptService.getDeptNo();
//		return new JsonResult(deptNo);
//	}
	
	//用于单个的树状图显示,包含不启用的状态，管理员显示
	@RequestMapping("/findalonedept.do")
	@ResponseBody
	public JsonObjectResult findAloneDept(HttpServletRequest request,String bindTop_dept_no,String send_dept_no){
		List<Object> list = deptService.findAloneDept(request,bindTop_dept_no,send_dept_no);
		return new JsonObjectResult(list);
	}
	
	//用于单位的树状图显示,全查，普通用户展示
	//20180503新建普通用户显示部门
	@RequestMapping("/findcommon.do")
	@ResponseBody
	public JsonObjectResult findAllDeptOfCommon(HttpServletRequest request,String send_dept_id){
	//	String org_id = request.getSession().getAttribute("org_id").toString();
	//	System.out.println("123"+org_id);
		List<Object> list = deptService.findAllDeptOfCommonUserNew(request, send_dept_id);
		return new JsonObjectResult(list);
	}
	
	//用于单个的树状图显示，普通用户展示
	@RequestMapping("/findalonecommon.do")
	@ResponseBody
	public JsonObjectResult findAloneCommon(HttpServletRequest request,String bindTop_dept_no,String send_dept_no){
		List<Object> list = deptService.findAloneCommon(request,bindTop_dept_no, send_dept_no);
		return new JsonObjectResult(list);
	}
	
	//20180503更新部门接口，分离增加部门
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonObjectResult newUpdateDept(HttpServletRequest request,Dept dept){
//		System.out.println("dept3="+dept);
		String mes = deptService.newUpdateDept(request, dept);
		return new JsonObjectResult(mes);
	}
	
	//20180625新增部门树接口，layui专用
	@RequestMapping("/laytree.do")
	@ResponseBody
	public JsonObjectResult layDeptTree(HttpServletRequest request,String state){
		List<Object> list = deptService.layDeptTree(request, state);
		return new JsonObjectResult(list);
	}
	
	
}
